extends Control

enum ROCKPAPERSCISSOR {
	ROCK,
	PAPER,
	SCISSOR
}

signal victory()

var choose: ROCKPAPERSCISSOR
var computer_choice: ROCKPAPERSCISSOR
var game_started = true
var animation_count = 0
var waiting_for_animation = false
var player_can_move = true
@onready var old_man: AnimatedSprite2D = %oldMan
@onready var player: AnimatedSprite2D = %player
@onready var label: Label = %Label

func _ready() -> void:
	get_tree().paused = true
	# 禁用玩家移动
	disable_player_movement()

func random() -> ROCKPAPERSCISSOR:
	var i = randi() % 3
	if i == 0:
		return ROCKPAPERSCISSOR.ROCK
	elif i == 1:
		return ROCKPAPERSCISSOR.PAPER
	elif i == 2:
		return ROCKPAPERSCISSOR.SCISSOR
		
	return ROCKPAPERSCISSOR.ROCK

func get_result() -> String:
	if choose == computer_choice:
		label.text = "平局！"
		return "平局！"
	elif (choose == ROCKPAPERSCISSOR.ROCK and computer_choice == ROCKPAPERSCISSOR.SCISSOR) or \
		 (choose == ROCKPAPERSCISSOR.PAPER and computer_choice == ROCKPAPERSCISSOR.ROCK) or \
		 (choose == ROCKPAPERSCISSOR.SCISSOR and computer_choice == ROCKPAPERSCISSOR.PAPER):
		RaffleManager.rocket_victory_count += 1

		label.text = "你赢了！"
		victory.emit()
		return "你赢了！"
	else:
		label.text = "你输了！"
		return "你输了！"

func play_game() -> void:
	if not game_started or waiting_for_animation:
		return
	computer_choice = random()
	waiting_for_animation = true
	animation_count = 0
	start_animation_sequence()

func start_animation_sequence() -> void:
	player.play("default")
	old_man.play("default")
	await player.animation_finished
	animation_count += 1
	print("动画播放第", animation_count, "次")
	
	if animation_count < 2:
		start_animation_sequence()
	else:
		# 两次动画播放完毕，显示结果
		show_result()

func show_result() -> void:
	var result = get_result()
	player.play(get_choice_name(choose))
	old_man.play(get_choice_name(computer_choice))
	print("玩家选择: ", get_choice_name(choose))
	print("电脑选择: ", get_choice_name(computer_choice))
	print("结果: ", result)
	waiting_for_animation = false
	if result == "你赢了！":
		# 处理抽奖奖励
		handle_raffle_reward()
	
	# 延迟关闭界面并恢复玩家移动
	await get_tree().create_timer(1.5).timeout
	close_rock_paper_scissors()

func disable_player_movement() -> void:
	if Utils.player:
		Utils.player.set_physics_process(false)
		player_can_move = false

func enable_player_movement() -> void:
	if Utils.player:
		Utils.player.set_physics_process(true)
		player_can_move = true

var raffle_result
func handle_raffle_reward() -> void:
	# 调用抽奖管理器进行抽奖
	raffle_result = RaffleManager.raffle()
	# 显示抽奖结果
	RaffleManager.show_raffle_result(raffle_result)
	
func close_rock_paper_scissors() -> void:
	# 恢复玩家移动
	enable_player_movement()
	if raffle_result:
		Utils.show_string_label_c("获得奖励: " + raffle_result.reward_description, Utils.player, Color.YELLOW)
	get_tree().paused = false
	# 关闭猜拳界面
	queue_free()

func get_choice_name(choice: ROCKPAPERSCISSOR) -> String:
	match choice:
		ROCKPAPERSCISSOR.ROCK:
			return "fist"
		ROCKPAPERSCISSOR.PAPER:
			return "paper"
		ROCKPAPERSCISSOR.SCISSOR:
			return "scissor"
		_:
			return "default"

func start_game() -> void:
	game_started = true
	RaffleManager.rocket_victory_count = 0
	print("猜拳游戏开始！")

func _on_scissor_button_pressed() -> void:
	choose = ROCKPAPERSCISSOR.SCISSOR
	play_game()

func _on_rock_button_pressed() -> void:
	choose = ROCKPAPERSCISSOR.ROCK
	play_game()

func _on_paper_button_pressed() -> void:
	choose = ROCKPAPERSCISSOR.PAPER
	play_game()
